package yxy.game.pm2.command.camptower;

import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.battle.BattleModule;
import yxy.game.pm2.module.battle.BattleType;
import yxy.game.pm2.module.camptower.CampTowerHandle;
import yxy.game.pm2.module.camptower.CampTowerModule;
import yxy.game.pm2.module.camptower.SettlementCampTower;
import yxy.game.pm2.module.fortdefeat.FortDefeatHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.Map;

/**
 * 阵营塔挑战结算
 *
 * @ClassName CMD_CampTowerChanllengeBattleClear
 * @Author jingtao-zheng
 * @Date 2022/8/2 12:28
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerChanllengeBattleClear, classtype = CampTowerMessage.CampTowerChanllengeBattleClearRequest.class)
public class CMD_CampTowerChanllengeBattleClear extends Command<CampTowerMessage.CampTowerChanllengeBattleClearRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerChanllengeBattleClearRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        String battleCode = request.getBattleCode();
        //获取阵营塔类型
        int type = request.getType().getNumber();
        CampTowerMessage.CampTowerChanllengeBattleClearResponse.Builder response = CampTowerMessage.CampTowerChanllengeBattleClearResponse.newBuilder();

        BattleModule battleModule = BattleModule.get(playerCode, battleCode);

        // 战斗已结算
        if (battleModule.isSettled()) {
            send(messageSource, response, Result.BattleHasSettled);
            return;
        }
        // 战斗尚未结束
        if (!battleModule.isFinished()) {
            send(messageSource, response, Result.BattleIsNotFinished);
            return;
        }
        // 战斗尚未结束
        BattleType battleType = CampTowerHandle.matchBattleType(type);
        CampTowerHandle handle = new CampTowerHandle(battleType);
        if (handle.isBattling(battleModule)) {
            if (!battleModule.isSkip()) {
                send(messageSource, response, Result.BattleIsNotFinished);
                return;
            }
        }

        response.setInfo(new BattleHandle().battleClear(battleModule));
        battleModule.setSettled(true);

        SettlementCampTower settlement = (SettlementCampTower) battleModule.getSettlement();
        response.setFloor(settlement.getFloor());
        CampTowerModule campTowerModule = CampTowerModule.get(playerCode);
        BattleResult battleResult = battleModule.getBattleResult();
        if (!battleResult.equals(BattleResult.LEFT_WIN)) {
            battleModule.set(FortDefeatHandle.battleExpireDay);
            //失败也扣次数
            Map<Integer, Integer> alltimes = campTowerModule.getTimes();
            Integer times = alltimes.get(type);
            alltimes.put(type, times - 1);
            campTowerModule.set();
            response.setStaramount(0);
            response.setTimes(times - 1);
            send(messageSource, response, Result.Success);
            return;
        }
        //记录排行榜
        settlement.settle();
        battleModule.set(FortDefeatHandle.battleExpireDay);
        //任务-记录通关最大层数
        Map<Integer, Integer> allfloor = campTowerModule.getFloor();
        Integer floor = allfloor.get(type);
        TaskType taskType = CampTowerHandle.matchTaskType2(type);
        TaskHandle.handle(playerCode, taskType, floor);
        //通关最大层数
        response.setBestfloor(floor);
        //剩余挑战次数
        Integer times = campTowerModule.getTimes().get(type);
        response.setTimes(times);
        //奖励
        response.addAllRewards(new CurrencyHandle().packItems(battleModule.getSettlement().reward()));
        response.setStaramount(settlement.getStarnum());
        send(messageSource, response, Result.Success);
        //TODO 暂时不知这个发布者有什么用
        //TowerPublisher.getInstance().publish(playerCode, towerModule.getFloor());
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerChanllengeBattleClearResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
